package com.ym.manager.admin.act;

import com.ym.manager.admin.act.model.Act;
import com.ym.manager.admin.goods.model.Goods;
import com.ym.utils.Common;
import com.ym.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActService {

    @Autowired
    private ActDao actDao;


    /**
     * 活动列表数据
     * @param pageNum
     * @param limit
     * @param merchant_id
     * @param name
     * @param status
     * @return
     */
    public Pages<Act> listData(int pageNum, int limit, int merchant_id, String name, int status,int type) {
        Map<String,Object> map = new HashMap<>();
        int begin = (pageNum-1)*limit;
        map.put("begin",begin);
        map.put("limit",limit);
        map.put("merchant_id",merchant_id);
        map.put("name",name);
        map.put("status",status);
        map.put("type",type);
        int count  = actDao.getListCount(map);
        List<Act> list = actDao.getList(map);
        Pages<Act> pages = new Pages<>(count,pageNum,limit);
        pages.setList(list);
        return pages;
    }

    /**
     * 获取添加活动关联商品列表
     * @param pageNum
     * @param limit
     * @param name
     * @param categoryId
     * @return
     */
    public Pages<Goods> addActGoods(int pageNum, int limit, String name, int categoryId) {
        Map<String,Object> map = new HashMap<>();
        int begin = (pageNum-1)*limit;
        map.put("begin",begin);
        map.put("limit",limit);
        if(Common.isNotEmpty(name)){
            map.put("name",name);
        }
        if(categoryId>0){
            map.put("categoryId",categoryId);
        }
        List<Goods> list = actDao.addActGoods(map);
        int count  = actDao.addActGoodsCount(map);
        Pages<Goods> pages = new Pages<>(count,pageNum,limit);
        pages.setList(list);
        return pages;
    }

    /**
     * 新增活动
     * @param act
     * @return
     */
    @Transactional
    public int insert(Act act) {
        int i =actDao.insert(act);
        if(i > 0){
            act.initJump();
            actDao.updateJump(act.getId(),act.getJump());
        }
        i += actDao.insertGoods(act.getId(), act.getPromoteGoods());
        return i;
    }

    /**
     * 修改活动
     * @param act
     * @return
     */
    @Transactional
    public int update(Act act) {
        int i =actDao.update(act);
        actDao.delGoods(act.getId());
        i += actDao.insertGoods(act.getId(), act.getPromoteGoods());
        return i;
    }

    /**
     * 活动详情
     * @param id
     * @return
     */
    public Act detail(int id) {
        Act detail = actDao.detail(id);
        List<String> promoteGoods = actDao.detailGoods(id);
        StringBuilder promoteGoodsStr = new StringBuilder();
        for(int i = 0;i<promoteGoods.size();i++){
            promoteGoodsStr.append(promoteGoods.get(i)).append(i == promoteGoods.size() - 1 ? "" : ",");
        }
        detail.setPromoteGoodsStr(promoteGoodsStr.toString());
        return detail;
    }

    /**
     * 活动上下架
     * @param id
     * @param status
     * @return
     */
    public int shelf(int id, int status) {
        return actDao.shelf(id,status);
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    public int delete(int id) {
        return actDao.delete(id);
    }
}
